package whf.framework.util.var;

import java.util.Date;
import java.util.Map;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.util.DateUtils;
import whf.framework.util.StringUtils;

/**
 * 日期时间变量容器
 * @author wanghaifeng
 * @create Jan 24, 2007 11:13:29 AM
 * 
 */
public class DatetimeVariableHolder implements VariableHolder{
	private static Log log = LogFactory.getLog(DatetimeVariableHolder.class);
	
	private final static String NOW = "NOW";
	
	private final static String YEAR 	= "YEAR";
	
	private final static String MONTH 	= "MONTH";
	
	private final static String DAY 	= "DAY";
	
	private final static String TODAY 	= "TODAY";
	
	private final static String YESTERDAY 	= "YESTERDAY";
	
	private final static String HOUR 	= "HOUR";
	
	private final static String MINUTE 	= "MINUTE";
	
	private final static String SECOND 	= "SECOND";

	public DatetimeVariableHolder(){
		log.debug(DatetimeVariableHolder.class);
	}
	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getAny()
	 */
	public Object getAny() {
		return this.getVariable(NOW);
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getHolder()
	 */
	public Map<String, Object> getHolder() {
		return null;
	}

	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getVariable(java.lang.String)
	 */
	public Object getVariable(String key) {
		if(StringUtils.equalsIgnoreCase(key, NOW)){
			return new Date();
		} else if(StringUtils.equalsIgnoreCase(key, YEAR)){
			return DateUtils.getYear(new Date());
		} else if(StringUtils.equalsIgnoreCase(key, MONTH)){
			DateUtils.Month month = DateUtils.getMonth(new Date());
			return DateUtils.Month.toInt(month) + 1;
		} else if(StringUtils.equalsIgnoreCase(key, DAY)){
			return DateUtils.getDay(new Date());
		} else if(StringUtils.equalsIgnoreCase(key, HOUR)){
			return DateUtils.get(new Date(), DateUtils.Field.HOUR);
		} else if(StringUtils.equalsIgnoreCase(key, MINUTE)){
			return DateUtils.get(new Date(), DateUtils.Field.MINUTE);
		} else if(StringUtils.equalsIgnoreCase(key, SECOND)){
			return DateUtils.get(new Date(), DateUtils.Field.SECOND);
		} else if(StringUtils.equalsIgnoreCase(key, TODAY)){
			Date d = new Date();
			d = DateUtils.setFields(d, new DateUtils.Field[]{
					DateUtils.Field.HOUR,
					DateUtils.Field.MINUTE,
					DateUtils.Field.SECOND,
					DateUtils.Field.MILLISECOND
			}, new int[]{0,0,0,0});
			return d;
		} else if(StringUtils.equalsIgnoreCase(key, YESTERDAY)){
			Date d = new Date();
			d = DateUtils.add(d, DateUtils.Field.DATE, -1);
			d = DateUtils.setFields(d, new DateUtils.Field[]{
					DateUtils.Field.HOUR,
					DateUtils.Field.MINUTE,
					DateUtils.Field.SECOND,
					DateUtils.Field.MILLISECOND
			}, new int[]{0,0,0,0});
			return d;
		} else {
			return null;
		}
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.util.var.VariableHolder#getScope()
	 */
	public VariableHolderScope getScope() {
		return VariableHolderScope.GLOBAL;
	}
	
}
